//
//  Number+PPKit.swift
//  PPKit
//
//  Created by xiaopin on 2019/8/5.
//  Copyright © 2019 PPKit. All rights reserved.
//  https://gitee.com/xiaopindev/PPKit
//  https://github.com/xiaopindev
//  https://xiaopin.cnblogs.com

/*
 由于在Swift中， Int Float Double CGFloat 之间不能直接运算，如果在代码中直接写转换代码，代码会写的有点多，看起来麻烦
 所以，这里扩展一下这几个数值类型的转换，使代码看起来更简洁
 实际开发中，我们默认使用Int和Double两种类型数据运算就好了
 
 //创建一个NumberFormatter对象
 let format = NumberFormatter()
 //设置numberStyle（有多种格式）
 format.numberStyle = .decimal
 let string = format.string(from: NSNumber(value: 1234567.89))
 print(string)

 //显示样式和输出样式
 numberStyle:
 public enum Style : UInt {
     case none //1234567.89
     case decimal //1,234,567.89
     case currency //￥1,234,567.89
     case percent //123,456,789%
     case scientific //1.23456789E6
     case spellOut //一百二十三万四千五百六十七点八九
     case ordinal //第123,4568
     case currencyISOCode //CNY1,234,567.89
     case currencyPlural//1,234,567.89人民币
     case currencyAccounting//￥1,234,567.89
 }
 */

import CoreFoundation
import Foundation

extension Int{
    var f:CGFloat{
        return CGFloat(self)
    }
    
    var d:Double{
        return Double(self)
    }
    
    var str:String {
        return String(self)
    }
    
    var scale: CGFloat {
        return CGFloat(self) * pp_widthRatio
    }
    
    /// 格式化：123456.89  to 123,456.89
    /// - Parameters:
    ///   - style: 样式，参考：Number+PPKit.swift注释说明
    ///   - prefix: 前缀：￥ 或者其他自定义
    ///   - suffix: 后缀：元 或者其他自定义
    /// - Returns: 格式化后的字符串
    func format(style:NumberFormatter.Style = .decimal,prefix:String? = nil,suffix:String? = nil) -> String?{
        let formatter = NumberFormatter()
        formatter.numberStyle = style
        if let value = prefix {
            formatter.positivePrefix = value
        }
        if let value = suffix {
            formatter.positiveSuffix = value
        }
        return formatter.string(from: NSNumber(value: self))
    }

}

extension CGFloat{    
    var d:Double{
        return Double(self)
    }
    
    var i:Int{
        return Int(self)
    }
    
    var str:String {
        return String(self.d)
    }
    
    var scale: CGFloat {
        return self * pp_widthRatio
    }
    
    var str_2f : String {
        return String(format: "%.2f", self)
    }
    
    var trimDotZeroString : String {
        let str = "\(self)"
        if str.hasSuffix(".0"){
            let int_value = Int(self)
            return "\(int_value)"
        }else{
            let result = self.str_2f
            if result.hasSuffix(".00"){
                return (result as NSString).substring(to:result.count-3)
            }else if result.hasSuffix("0"){
                return (result as NSString).substring(to:result.count-1)
            }else {
                return result
            }
        }
    }
    
}

extension Double{
    var f:CGFloat{
        return CGFloat(self)
    }
    
    var i:Int{
        return Int(self)
    }
    
    var str:String {
        return String(self)
    }
    
    var cleanZero : String {
        return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : self.truncatingRemainder(dividingBy: 0.1) == 0 ? String(format: "%.1f", self) : String(format: "%.2f", self)
    }
    ///会默认做四舍五入
    var str_2f : String {
        return String(format: "%.2f", self)
    }
    
    ///会默认做四舍五入
    var str_0f : String {
        return String(format: "%.0f", self)
    }
    
    ///向上取整
    var ceil_int_value : Int {
        let ceil_value = ceil(self)
        return Int(ceil_value)
    }
    
    var str_2f_fabs : String {
        return fabs(self).str_2f
    }
    
    var str_2f_decimal:Any {
        if str_2f.hasSuffix(".00"){
            let str_int = (str_2f as NSString).substring(to:str_2f.count-3)
            return Double(Int(str_int)!)
        }
        return Decimal(string:str_2f)!
    }
    
    var trimDotZeroString : String {
        let str = "\(self)"
        if str.hasSuffix(".0"){
            let int_value = Int(self)
            return "\(int_value)"
        }else{
            let result = self.str_2f
            if result.hasSuffix(".00"){
                return (result as NSString).substring(to:result.count-3)
            }else if result.hasSuffix("0"){
                return (result as NSString).substring(to:result.count-1)
            }else {
                return result
            }
        }
    }
    
    var scale: CGFloat {
        return CGFloat(self) * pp_widthRatio
    }
    
    /// 格式化：123456.89  to 123,456.89
    /// - Parameters:
    ///   - style: 样式，参考：Number+PPKit.swift注释说明
    ///   - prefix: 前缀：￥ 或者其他自定义
    ///   - suffix: 后缀：元 或者其他自定义
    /// - Returns: 格式化后的字符串
    func format(style:NumberFormatter.Style = .decimal,prefix:String? = nil,suffix:String? = nil) -> String?{
        let formatter = NumberFormatter()
        formatter.numberStyle = style
        formatter.maximumFractionDigits = 2
        formatter.minimumFractionDigits = 2
        if let value = prefix {
            formatter.positivePrefix = value
        }
        if let value = suffix {
            formatter.positiveSuffix = value
        }
        return formatter.string(from: NSNumber(value: self))
    }
    
    ///  四舍五入
    /// - Parameter places: 保留几位小数
    /// - Returns: <#description#>
    public func roundTo(places: Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
}

extension Int32{
    var i:Int{
        return Int(self)
    }
}

extension Int64{
    var i:Int{
        return Int(self)
    }
}

extension String{
    var f:CGFloat{
        return CGFloat(self.d)
    }
    
    var i:Int{
        if self.isNullOrEmpty() {return 0}
        if self.isPureInt(){
            return Int(self)!
        }
        return 0
    }
    
    var d:Double {
        if self.isNullOrEmpty() {return 0}
        if self.isPureInt(){
            return Double(self)!
        }
        return 0
    }
}

